/**
 * 使用函数或对象：
 * FWK.DataBox | FWK.Ajax | Function.prototype.bind | FWK.isStr | FWK.checkResponse | FWK.isObj | selectNodes | FWK.extend | FWK.Class
 */
XN = window.XN || {};
XN.Res = XN.DataBox.extend({
	ress:{photo:0,music:1,video:2,note:3},
	urls:["/photoAlbums.xml","/musicAlbums.xml","/videoAlbums.xml","/noteAlbums.xml"],
	folders:["/photos/","/musics/","/videos/","/notes/"],
	doKeys:["/resource/photo.do?m=query","/resource/music.do?m=query","/resource/video.do?m=query","/resource/note.do?m=query"],
	data:{},
	$init:function(_res,_userId,_data){//photo | music | video | note
		this.res = this.ress[_res];
		this.userId = _userId;
		this.data = _data || this.data;
		if(!this.data.contentList){
			this.$init.superset.call(this,"/res/public/userfile" + XN.toPath(this.userId,5,100) + this.urls[this.res],"POST");
			this.askForData();
		}
		else{
			try{this.returnGroup.delay(0.01,[],this);}catch(e){}
		}
	},
	returnGroup:function(){
		this.$returnGroup();
	},
	$completeData:function(){
		if(FWK.isNum(this.indexId)){
			this.data[this.indexId] = this.toObj("/items/contentList/item");
			try{this.$returnItem();}catch(e){}
		}
		else{
			this.data.systemList = this.toObj("/albums/systemList/item");
			this.data.contentList = this.toObj("/albums/contentList/item");
			try{this.$returnGroup();}catch(e){}
		}
		this.record = null;
	},
	askForItems:function(_groupId,_privilege,_flag){
		if(_privilege==1){
			this.url = "/res/public/userfile" + XN.toPath(this.userId,5,100) + this.folders[this.res] + _groupId + ".xml";
		}
		else{
			this.url = this.doKeys[this.res] + "&albumId=" + _groupId;
		}
		this.indexId = parseInt(_groupId);
		if(!_flag && this.data[_groupId]){
			try{this.$returnItem();}catch(e){}
		}
		else{
			this.data[this.indexId] = null;
			this.askForData();
		}
	},
	toObj:function(_xPath){
		var obj = {},_arr = this.record.selectNodes(_xPath);
		for(var i=0,ilen=_arr.length;i<ilen;i+=1){
			obj[_arr[i].getAttribute("id")] = _arr[i];
		}
		obj.length = i;
		return obj;
	},
	//数据跟新部分
	delGroup:function(_delId,_toId){
		if(_toId){
			this.data.contentList[_toId].setAttribute("count",parseInt(this.data.contentList[_toId].getAttribute("count")) + parseInt(this.data.contentList[_delId].getAttribute("count")));
			if(this.data[_toId] && this.data[_delId]){
				FWK.extend(this.data[_toId],this.data[_delId]);
				this.data[_toId].length = Number(this.data.contentList[_toId].getAttribute("count"));
			}
		}
		this.data.contentList[_delId] = null;
		delete this.data.contentList[_delId];
		if(this.data[_delId]){
			this.data[_delId] = null;
			delete this.data[_delId];
		}
		this.data.contentList.length-= 1;
	},
	delItem:function(_groupId,_itemId){
		this.data[_groupId][_itemId] = null;
		delete this.data[_groupId][_itemId];
		this.data[_groupId].length-=1;
		this.editCount(_groupId,-1);
	},
	saveGroup:function(_xml){
		var id = _xml.getAttribute("id");
		if(!this.data.contentList[id]){
			this.data.contentList.length+= 1;
		}
		this.data.contentList[id] = _xml;
		id = null;delete id;
	},
	saveItem:function(_groupId,_xml){
		this.editCount(_groupId,1);
		if (!this.data[_groupId]) {
			return;
		}
		var id = _xml.getAttribute("id");
		if(!this.data[_groupId][id]){
			this.data[_groupId].length+=1;
		}
		this.data[_groupId][id] = _xml;
		_xml = null;delete _xml;
		id = null;delete id;
	},
	moveItem:function(_itemId,_fromId,_toId){
		if(_fromId == _toId){
			return;
		}
		if(this.data[_toId]){
			this.data[_toId][_itemId] = this.data[_fromId][_itemId];
			this.data[_toId].length+=1;
		}
		this.editCount(_toId,1);
		this.editCount(_fromId,-1);
		this.data[_fromId][_itemId] = null;
		delete this.data[_fromId][_itemId];
		this.data[_fromId].length-=1;
	},
	editCount:function(_id,_num){
		if(this.data.contentList[_id]){
			var count = parseInt(this.data.contentList[_id].getAttribute("count"));
			this.data.contentList[_id].setAttribute("count",count + _num);
		}
		else if(this.data.systemList[_id]){
			var count = parseInt(this.data.systemList[_id].getAttribute("count"));
			this.data.systemList[_id].setAttribute("count",count + _num);
		}
		count = null;delete count;
	},
	//重新请求数据
	reLoadGroup:function(){
		this.url = "/res/public/userfile" + XN.toPath(this.userId,5,100) + this.urls[this.res];
		this.indexId = null;
		this.data = {};
		this.askFordata();
	},
	$errData:function(_ret){
		if(_ret.code==-1){
			if(FWK.isNum(this.indexId)){
				this.data[this.indexId] = {length:0};
				try{this.$returnItem();}catch(e){}
			}
			else{
				this.data.systemList = {length:0};
				this.data.contentList = {length:0};
				try{this.$returnGroup();}catch(e){}
			}
		}
		else{
			alert(_ret.des);
		}
	}
});
